package com.blog.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.blog.service.interfaces.IQiniu;
import com.qiniu.api.auth.AuthException;

@Controller
@RequestMapping("/file")
public class FileController {
	@Autowired
	IQiniu qiniuServiceImpl;
 
	
	@RequestMapping("/upload")
	@ResponseBody
	public Map<String,String> upload(@RequestParam(required = false)MultipartFile file33) throws AuthException, JSONException, IOException{
		Map<String,String> map =new HashMap<String,String>();
		 
		
		System.out.println("接收到文件");
		//设置AccessKey
		qiniuServiceImpl.setAccessKey("s2fsRftmBIsuv4n7wDbsRdg9ugzcwlgjIbEc2MO-");
        //设置SecretKey
		qiniuServiceImpl.setSecretKey("D-En304rQlQg5hwqJcxUBHgY3DSy2cUNTL7k-dDT");
        //设置存储空间
		qiniuServiceImpl.setBucketName("dmxdpw");
        //设置七牛域名
		qiniuServiceImpl.setDomain("7xkz5s.com1.z0.glb.clouddn.com");
		
		String newName = "uuid_"+qiniuServiceImpl.getUUID()+".jpg";
		
		boolean state=qiniuServiceImpl.uploadFile(newName, file33.getInputStream());
		if(state==false){map.put("code","1");}else{map.put("code","0");map.put("url","http://7xkz5s.com1.z0.glb.clouddn.com/"+newName);}
		
		System.out.println(map);
		
		return map;
		
	}
	
	@RequestMapping("/upload2")
	@ResponseBody
	public void upload2(Object  file) throws AuthException, JSONException, IOException{
		System.out.println(file.getClass());
		System.out.println(file.getClass().getName());
		System.out.println(file.getClass().getMethods());
	}
	
	@RequestMapping("/upload3")
	@ResponseBody
	public void upload3(String file) throws AuthException, JSONException, IOException{
		System.out.println(file);
	}
	
	@RequestMapping(value="/getjson")
	@ResponseBody
	public Map<String, String> getjson3(){
		System.out.println("访问了3");
		Map<String,String> resultMap = new HashMap<String, String>(); 
			resultMap.put("result", "得到了json"); 
			resultMap.put("result2", "得到了jso2"); 
	        return resultMap;  
	}
}
